//
//  YZMTradeFilterView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/5/30.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMTradeFilterView.h"
#import <ColorUtils/ColorUtils.h>
#import "Masonry+Utils.h"

static const float YZMTradeFilterViewForCellHeight = 50.0;

@interface YZMTradeFilterView ()

@property (nonatomic, strong) NSMutableArray *typesList;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *bgView;

@end

@implementation YZMTradeFilterView

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        self.typesList = [NSMutableArray array];
        self.backgroundColor = [UIColor clearColor];
        
        self.bgView = [UIView new];
        self.bgView.backgroundColor = [UIColor blackColor];
        self.bgView.alpha = 0.1;
        [self addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        self.scrollView = [[UIScrollView alloc] init];
        self.scrollView.backgroundColor = [UIColor whiteColor];
        [self addSubview: self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.width.equalTo(self);
        }];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
        [self.bgView addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)tapGestureAction{
    
    [self hide];
    if (self.hideHandler) {
        
        self.hideHandler();
    }
}


- (UIButton *)choiceButtonInSubview
{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    [self.scrollView addSubview: button];
    [button setTitleColor:[YZMAppStyle colorForLabelMark] forState:UIControlStateNormal];
    [button.titleLabel setFont:[YZMAppStyle largeSizeFont]];
    [button addTarget: self action: @selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
    
    return button;
}

- (void)createDefault
{
    UIView *lastView = nil;
    for (NSArray *part in _typesList) {
        
        NSInteger index = [_typesList indexOfObject:part];
        NSString *title = part[0];
        
        //分割线
        if (index != 0) {
            
            UIView *line = [YZMAppStyle splitLine];
            [self.scrollView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.width.equalTo(lastView);
                make.top.equalTo(lastView.mas_bottom);
            }];
        }
        
        //按钮
        UIButton *button = [self choiceButtonInSubview];
        [button setTitle:title forState:UIControlStateNormal];
        button.tag = [part[1] integerValue];
        [self.scrollView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.width.equalTo(self.scrollView);
            
            if (lastView) {
            
                make.top.equalTo(lastView.mas_bottom);
                make.height.equalTo(lastView);
            }else{
                make.top.equalTo(self.scrollView);
                make.height.mas_equalTo(YZMTradeFilterViewForCellHeight);
            }
        }];
        
        lastView = button;
    }
    
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(lastView);
    }];
}

- (void)loadButtonsForTypes: (NSArray *)types
{
    [self.typesList removeAllObjects];
    [self.typesList addObjectsFromArray:types];
    [self createDefault];
    
    [self layoutIfNeeded];
    
    [self  show];
}

- (void)show{
    
    self.hidden = NO;
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.typesList.count * YZMTradeFilterViewForCellHeight);
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.bgView.alpha = 0.5;
        [self layoutIfNeeded];
        
    }completion:^(BOOL result){
        
    }];
}

- (void)hide{
    
    
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.bgView.alpha = 0.1;
        [self layoutIfNeeded];
        
    }completion:^(BOOL result){
        
        self.hidden = YES;
    }];
}

- (void)buttonPressed: (id)sender
{
    NSInteger tag = [sender tag];
    [self hide];
    if (self.filterHandler) {
        self.filterHandler(tag);
    }
}

@end
